package com.hzit.loan.pay.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

public class MqConfig {


    public static final String PAY_NOTIFY_EXCHANGE_NAME = "PAY_NOTIFY_EXCHANGE_NAME";


    public static final String DEAD_PAY_NOTIFY_EXCHANGE_NAME = "DEAD_PAY_NOTIFY_EXCHANGE_NAME";


    public static final String PAY_NOTIFY_QUEUE_NAME = "payNotifyQueue";


    public static final String DEAD_LETTER_PAY_NOTIFY_ROUTING_KEY = "payNptify_queue_routing_key";


    public static final String DEAD_PAY_NOTIFY_QUEUE_NAME = "dead_payNotifyQueue";



    /**
     * 声明Exchange
     * @return
     */
    @Bean("payNotifyExchange")
    public FanoutExchange payNotifyExchange(){
        return new FanoutExchange(PAY_NOTIFY_EXCHANGE_NAME);
    }

    // 声明死信Exchange
    @Bean("deadpayNotofyExchange")
    public DirectExchange deadLetterExchange(){

        return new DirectExchange(DEAD_PAY_NOTIFY_EXCHANGE_NAME);
    }


    /**
     *
     * @return
     */
    @Bean("payNotifyQueue")
    public Queue payNotifyQueue(){
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_PAY_NOTIFY_EXCHANGE_NAME);
//       x-dead-letter-routing-key  这里声明当前队列的死信路由key

        args.put("x-message-ttl", 12000);
        args.put("x-dead-letter-routing-key", DEAD_LETTER_PAY_NOTIFY_ROUTING_KEY);
        return QueueBuilder.durable(PAY_NOTIFY_QUEUE_NAME).withArguments(args).build();
    }


    /**
     *  声明死信队列A
     * @return
     */
    @Bean("deadPayNotifyQueue")
    public Queue deadPayNotifyQueue(){
        return new Queue(DEAD_PAY_NOTIFY_QUEUE_NAME);
    }



    @Bean
    public Binding businessBindingA(@Qualifier("payNotifyQueue") Queue queue,
                                    @Qualifier("payNotifyExchange") FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }


    // 声明死信队列A绑定关系
    @Bean
    public Binding deadLetterBindingB(@Qualifier("deadPayNotifyQueue") Queue queue,
                                      @Qualifier("deadpayNotofyExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_PAY_NOTIFY_ROUTING_KEY);
    }

}
